package com.test.lucifer;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.JsonNodeReader;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Assert;
import org.junit.Test;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * JsonSchema
 *
 * @Author yuxiao05@meituan.com
 * @Create 2016-07-14 下午4:38
 */

public class testJsonSchema {

  /**
   * JSON Schema validator
   */
  @Test
  public void testJsonSchema1() {

    // JsonNode schema = transfer2JsonNode(schema1);
    // JsonNode data = transfer2JsonNode(successObj);
    int times = 1000;
    long start = 0, end = 0, sum = 0;
    for (int i = 0; i < times; i++) {
      JsonNode schema = readJsonFile("src/main/resources/Schema.json");
      JsonNode data = null;
      if (i % 2 == 0) {
        data = readJsonFile("src/main/resources/failure.json");
      }
      else data = readJsonFile("src/main/resources/successObj.json");
      start = System.currentTimeMillis();
      ProcessingReport report = JsonSchemaFactory.byDefault().
              getValidator().validateUnchecked(schema, data);
      end = System.currentTimeMillis();
      sum += end - start;
      // Assert.assertTrue(report.isSuccess());

    }
    System.out.println(sum);
  }

  private JsonNode readJsonFile(String filePath) {
    JsonNode instance = null;
    try {
      instance = new JsonNodeReader().fromReader(new FileReader(filePath));
    } catch (IOException e) {
      e.printStackTrace();
    }
    return instance;
  }

  @Test
  public void testJsonSchema2() {
    String failure = new String("{\"foo\":1234}");
    String Schema = "{\"type\": \"object\", \"properties\" : {\"foo\" : {\"type\" : \"string\"}}}";
    ProcessingReport report = null;
    try {
      JsonNode data = JsonLoader.fromString(failure);
      JsonNode schema = JsonLoader.fromString(Schema);
      report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
    } catch (IOException e) {
      e.printStackTrace();
    }
    //Assert.assertTrue(report.isSuccess());
    Iterator<ProcessingMessage> it = report.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }

  @Test
  public void testJsonSchema3() {
    int times = 1000;
    long start = 0, end = 0, sum = 0;
    for (int i = 0; i < times; i++) {
      InputStream inputStream = getClass().getResourceAsStream("/Schema.json");
      JSONObject Schema = new JSONObject(new JSONTokener(inputStream));
      JSONObject data = null;
      if (i % 2 == 0)
        data = new JSONObject("{\"foo\" : 1234}");
      else data = new JSONObject("{\"foo\" : \"bar\"}");
      try {
        start = System.currentTimeMillis();
        Schema schema = SchemaLoader.load(Schema);
        schema.validate(data);
        end = System.currentTimeMillis();
        sum += (end - start);
      } catch (ValidationException e) {
        //System.out.println(e.getMessage());
      }
    }
    System.out.println(sum);
  }
}
